<template>
    <div class="user-info-container">
      <!-- 返回箭头 -->
      <div class="back-arrow" @click="goBack">
        <svg class="arrow-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
          <path d="M15 18l-6-6 6-6"></path>
        </svg>
      </div>
      <div v-if="loading" class="loading">
        <div class="spinner"></div>
        加载中...
      </div>
      <div v-else-if="error" class="error">
        <svg class="error-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
          <circle cx="12" cy="12" r="10"></circle>
          <path d="M12 8v4"></path>
          <path d="M12 16h.01"></path>
        </svg>
        {{ error }}
      </div>
      <div v-else class="content-wrapper">
        <!-- 用户选择 -->
        <select v-model="selectedUserIndex" class="user-select">
          <option v-for="(user, index) in users" :key="index" :value="index">
            用户 {{ index + 1 }} ({{ user.username || '未知用户' }})
          </option>
        </select>
        <!-- 用户信息 -->
        <div class="user-info-card">
          <div class="user-info">
            <div class="labels">
              <div class="label">用户名</div>
              <div class="label">总学习时长</div>
              <div class="label">开始活跃时间</div>
              <div class="label">最后活跃时间</div>
              <div class="label">活跃状态</div>
            </div>
            <div class="values">
              <div class="value">{{ userInfo.username || '未知用户' }}</div>
              <div class="value">{{ userInfo.totalStudyTime }} 小时</div>
              <div class="value">{{ userInfo.startActiveTime || '未记录' }}</div>
              <div class="value">{{ userInfo.lastActiveTime || '未记录' }}</div>
              <div class="value status-value">
                <svg v-if="userInfo.isActive" class="status-icon active" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                  <path d="M20 6L9 17l-5-5"></path>
                </svg>
                <svg v-else class="status-icon inactive" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                  <path d="M18 6L6 18M6 6l12 12"></path>
                </svg>
                {{ userInfo.isActive ? '在线' : '离线' }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref, onMounted, computed } from 'vue';
  import { useRouter } from 'vue-router'; // 如果使用 vue-router
  import { getContainerList } from '@/api/stu/stu.ts';
  
  const users = ref([]);
  const selectedUserIndex = ref(0);
  const loading = ref(true);
  const error = ref('');
  
  // 如果使用 vue-router
  const router = useRouter();
  
  const formatTime = (timestamp) => {
    if (!timestamp) return '';
    const date = new Date(timestamp);
    return date.toLocaleString('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit'
    });
  };
  
  // 计算当前选中的用户信息
  const userInfo = computed(() => {
    return users.value[selectedUserIndex.value] || {
      username: '',
      totalStudyTime: 0,
      startActiveTime: '',
      lastActiveTime: '',
      isActive: false
    };
  });
  
  // 返回上一个页面
  const goBack = () => {
    // 如果使用 vue-router
    router.go(-1);
    // 如果不使用 vue-router，可以使用 window.history
    // window.history.back();
  };
  
  onMounted(async () => {
    try {
      const result = await getContainerList();
      console.log("获取原始数据成功:", result);
  
      if (!result || !Array.isArray(result.data)) {
        throw new Error('后端返回的数据格式不正确');
      }
  
      users.value = result.data.map(userData => ({
        username: userData.username || '未知用户',
        totalStudyTime: Math.floor(userData.totalTime / (1000 * 60 * 60)) || 0,
        startActiveTime: formatTime(userData.startTime),
        lastActiveTime: formatTime(userData.lastActivityTime),
        isActive: userData.status === 'ACTIVE'
      }));
    } catch (err) {
      console.error("获取用户信息失败:", err);
      error.value = '获取用户信息失败，请稍后重试';
    } finally {
      loading.value = false;
    }
  });
  </script>
  
  <style scoped>
  .user-info-container {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background: linear-gradient(135deg, #d6eaff 0%, #f0e4ff 100%);
    padding: 40px 20px;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  }
  
  /* 返回箭头 */
  .back-arrow {
    position: absolute;
    top: 20px;
    left: 20px;
    cursor: pointer;
    padding: 10px;
    border-radius: 50%;
    background: #ffffff;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    transition: background 0.3s ease, transform 0.3s ease;
  }
  
  .back-arrow:hover {
    background: #f7fafc;
    transform: scale(1.1);
  }
  
  .arrow-icon {
    width: 24px;
    height: 24px;
    color: #4a5568;
  }
  
  .content-wrapper {
    width: 100%;
    max-width: 600px;
    animation: fadeIn 0.5s ease-in-out;
  }
  
  .user-info-card {
    background: #ffffff;
    border-radius: 20px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.08);
    padding: 35px;
    transition: transform 0.3s ease, box-shadow 0.3s ease;
    margin-top: 25px;
  }
  
  .user-info-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 15px 50px rgba(0, 0, 0, 0.12);
  }
  
  .user-info {
    display: flex;
    justify-content: space-between;
  }
  
  .labels,
  .values {
    display: flex;
    flex-direction: column;
    gap: 25px;
  }
  
  .labels {
    font-weight: 600;
    color: #2d3748;
    font-size: 17px;
  }
  
  .values {
    color: #5a677a;
    text-align: right;
    font-size: 17px;
  }
  
  .label,
  .value {
    line-height: 1.6;
  }
  
  .status-value {
    display: flex;
    align-items: center;
    gap: 10px;
  }
  
  .status-icon {
    width: 18px;
    height: 18px;
  }
  
  .status-icon.active {
    color: #2f855a;
  }
  
  .status-icon.inactive {
    color: #a0aec0;
  }
  
  .active {
    color: #2f855a;
    font-weight: 600;
  }
  
  .inactive {
    color: #a0aec0;
    font-weight: 600;
  }
  
  .user-select {
    width: 100%;
    padding: 14px 18px;
    font-size: 16px;
    border: 1px solid #e2e8f0;
    border-radius: 10px;
    background: #fff;
    color: #2d3748;
    box-shadow: 0 3px 15px rgba(0, 0, 0, 0.05);
    transition: border-color 0.3s ease, box-shadow 0.3s ease;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%234a5568' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 14px center;
    background-size: 18px;
  }
  
  .user-select:focus {
    outline: none;
    border-color: #5a67d8;
    box-shadow: 0 0 0 3px rgba(90, 103, 216, 0.1);
  }
  
  .user-select:hover {
    border-color: #a0aec0;
  }
  
  .loading {
    display: flex;
    align-items: center;
    gap: 14px;
    font-size: 18px;
    color: #4a5568;
    background: #fff;
    padding: 20px 25px;
    border-radius: 10px;
    box-shadow: 0 3px 15px rgba(0, 0, 0, 0.1);
  }
  
  .spinner {
    width: 28px;
    height: 28px;
    border: 4px solid #e2e8f0;
    border-top: 4px solid #5a67d8;
    border-radius: 50%;
    animation: spin 1s linear infinite;
  }
  
  @keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
  }
  
  .error {
    display: flex;
    align-items: center;
    gap: 14px;
    background: #fff5f5;
    color: #c53030;
    padding: 20px 25px;
    border-radius: 10px;
    font-size: 16px;
    box-shadow: 0 3px 15px rgba(0, 0, 0, 0.1);
    animation: shake 0.5s ease;
  }
  
  .error-icon {
    width: 28px;
    height: 28px;
    color: #c53030;
  }
  
  @keyframes fadeIn {
    from { opacity: 0; transform: translateY(15px); }
    to { opacity: 1; transform: translateY(0); }
  }
  
  @keyframes shake {
    0%, 100% { transform: translateX(0); }
    10%, 30%, 50%, 70%, 90% { transform: translateX(-5px); }
    20%, 40%, 60%, 80% { transform: translateX(5px); }
  }
  
  /* 响应式设计 */
  @media (max-width: 600px) {
    .content-wrapper {
      padding: 0 15px;
    }
  
    .user-info-card {
      padding: 25px;
    }
  
    .user-info {
      flex-direction: column;
      gap: 20px;
    }
  
    .labels,
    .values {
      text-align: left;
    }
  
    .values {
      color: #5a677a;
    }
  
    .user-select {
      font-size: 15px;
      padding: 12px 16px;
    }
  
    .back-arrow {
      top: 15px;
      left: 15px;
    }
  
    .arrow-icon {
      width: 20px;
      height: 20px;
    }
  }
  </style>